<template>
  <div class="el-mr-shandover">
    <div>
      <TableTitle>
        <slot>
          <p>
            流程进度
          </p>
        </slot>
      </TableTitle>
      <div>
      <li>
        <el-button type="success" icon="el-icon-refresh" @click="refresh" size="mini">刷新</el-button>
      </li>  
    </div>
      <div>
        <el-steps :active="active" finish-status="success" style="margin-top: 10px;" align-center>
          <el-step title="扫描工号" ></el-step>
          <el-step title="扫描条码" ></el-step>
        </el-steps>
        <!-- <el-button style="margin-top: 12px;" @click="next">下一步</el-button> -->
      </div>
      <TableTitle>
        <slot>
          <p>
            扫码操作
          </p>
        </slot>
      </TableTitle>
      <TopSearch ref="topSearching">
        <slot>
          <li>
            <el-button size="mini" type="primary" @click="scanning"
              >扫码移交</el-button
            >
          </li>
          <li>
            <el-button size="mini" type="primary" @click="resetEmpcode"
              >重置</el-button
            >
          </li>
          <li>
            <p>扫码人工号：{{smPerson}}</p>
          </li>
        </slot>
      </TopSearch>
    </div>
    <div>
    <TableTitle>
      <slot>
        <p>
          查询条件
        </p>
      </slot>
    </TableTitle>
      <MidSearch ref="midScan"></MidSearch>
    </div>
    <TableTitle>
      <slot>
        <p>
          已移交物料列表
        </p>
      </slot>
    </TableTitle>
    <Table @materialId="materialId"></Table>
  </div>
</template>
<script>
import { receiveAndRjectcive, mrMhAll } from "@/utils/api.js";
import TopSearch from "../../../../components/TopSearch.vue";
import Table from "./components/Tables.vue";
import MidSearch from "./components/MidSearch.vue";
import TableTitle from "../../../../components/TableTitle.vue"
import request from "@/utils/request";

export default {
  name: "ScanningHandover",
  inject:['reload'],
  components: {
    TopSearch,
    Table,
    MidSearch,
    TableTitle
  },
  data() {
    return {
      resData: "",
      totalCount: "",
      idList: [],
      pageSize: 20,
      pageNumber: 1,
      tiaoma: "",
      smPerson:'',
      // 步骤条
      active: 0,
    };
  },
  methods: {
    next() {
        if (this.active++ > 0){
          setTimeout(()=>{
            this.active = 1
          },1000)
        }
        console.log(this.active)
      },
      resetEmpcode(){
        this.smPerson = "";
        this.$bus.$emit("resetScanInput",{});
      },
    scanning() {
      if(this.smPerson == ''){
        request.post("/WmsDeliverPlan/searchWorkerCode",{"smPerson":this.tiaoma}).then((res)=>{
          console.log('res',res)

          if(res.data.retCode=='201'){
            this.$Alert({
              title: "失败",
              message: res.data.message,
              type: "error",
          });
          }else{
            this.$Alert({
              title: "成功",
              message: res.data.message,
              type: "success",
          })
            this.smPerson = this.tiaoma
            this.$options.methods.next.bind(this)()
          }
        });
      }else if(this.tiaoma.length==6){//yebo 重复输人员工号顶替之前的
        this.smPerson = this.tiaoma;
        this.$options.methods.next.bind(this)();
      }else{
        let barcode = '';
        var barcodeType = "",bzjCodeType="";
        if(this.tiaoma.includes('{')){
            var tiaomaObj = JSON.parse(this.tiaoma);
            if(tiaomaObj.hasOwnProperty("CPHGZ")){//成品
              barcode = tiaomaObj.CPHGZ;
              barcodeType = "CP";
            }else{//标件
              barcode = tiaomaObj.CKCODE;
              barcodeType = "BJ";
              bzjCodeType = tiaomaObj.BARCODETYPE;//BZJCK  BZJCKAO
            }
        }else{//85的零件不是纯数字 GW-222222
          barcode = this.tiaoma;
          barcodeType = "LJ";
        }

        
        // console.log(barcode)
        receiveAndRjectcive({
            barcode:barcode,
            funcType:'transfer',
            userCode:this.smPerson,
            barcodeType:barcodeType,
            bzjCodeType:bzjCodeType
        }).then((res) => {
          if(res.data.retCode == 200){
            // 进度条
            this.$options.methods.next.bind(this)()
            this.$Alert({
              title: "成功",
              message: res.data.message,
              type: "success",
            });
            // 更新数据
            this.$refs.midScan.scanningSearch()
            // 扫码清空 
            this.$refs.topSearching.openScan()
            // 重新聚焦
            this.$refs.topSearching.makeTmNull()
          }else if(res.data.retCode == 201) {
            // alert(this.errorInfo,201)
            this.$Alert({
              title: "错误",
              message: res.data.message,
              type: "error",
            });
          }
        });
      }
    },
    refresh() {
    console.log("刷新");
		this.reload();
    },
    materialId(value) {
      // 接收Table勾选的信息info
      this.idList = value;
    },
  },
  mounted() {
    // 接收TopSearch的条码
    this.$bus.$on("saoma", (e) => {
     // {"XH":"JT27/A42-0003","JH":"22003","CPHGZ":"CP-640-12-13-1"} 返回格式
        this.tiaoma = e
        if(this.smPerson != ''){
          this.$bus.$emit('mrShSearchBarcode',e)
        }
    });
    //yebo 2023-05-25 新增定时任务刷新工号
  /*  var that = this;
    window.setInterval(function(){
        console.log("定时任务");
        that.smPerson = "";
    },1000*60*10);//十分钟刷新一次*/
  
    
  },
};
</script>
<style scoped>
.el-mr-sdSearchTitle {
  background-color: rgb(57, 135, 199);
  color: white;
  padding: 5px 2px;
}
</style>